//
//  JHStatusListViewModel.swift
//  JHSinaWeibo
//
//  Created by apple on 16/6/11.
//  Copyright © 2016年 apple. All rights reserved.
//  给tableView提供数据

import UIKit

class JHStatusListViewModel: NSObject {
//定义数组模型
    lazy var statusViewModelArray:[JHStatusViewModel] = [JHStatusViewModel]()
    //加载数据
    func loadData(finished:(isSuccess :Bool) -> ()){
        JHNetWorkTools.sharedTool.loadHomePageData { (responseObject, error) in
            if error != nil {
              //  SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            print(responseObject)
            //处理AFN已经将数据处理成了oc之后数组或者字典
            guard let dict  = responseObject as? [String:AnyObject]else{
                return
            }
            //根据statuses获取数组
            guard  let array = dict ["statuses"] as?
                [[String:AnyObject]] else{
                    return
            }
            print(array)
            //遍历数组 获取单个字典
            //实例化控数组
            var tempArray = [JHStatusViewModel]()
            for item in array{
                //字典转模型
                let s = JHStatus(dict:item)
                //在数组中添加视图模型
                let viewModel  = JHStatusViewModel(status:s)
                //添加到数组中
                tempArray.append(viewModel)
            }
            //赋值保存
            self.statusViewModelArray = tempArray
            //刷新数据
            //self.tableView.reloadData()
            //执行成功的回调
            finished(isSuccess: true)
        }
        
    }
}


